import vtkmodules.all as vtk
from CloudEngine.Math.Vector3 import Vector3


class TLine:
    id = -1
    start = None
    end = None
    edgeType = 'FREE'

    def __init__(self, start: Vector3, end: Vector3, line_id=None, edgeType=None):

        self.start = start
        self.end = end
        if line_id is not None:
            self.id = line_id
        if edgeType is not None:
            self.edgeType = edgeType

    def get_depth(self):
        return self.start.z, self.end.z

    def distanceFromPoint(self, point: Vector3) -> int:
        # A是start起始点 B是end终点 P是point点
        # 计算向量 AB 和 AP
        AB = Vector3(self.end.x - self.start.x, self.end.y - self.start.y,
                     self.end.z - self.start.z)
        AP = Vector3(point.x - self.start.x, point.y - self.start.y,
                     point.z - self.start.z)

        # 计算向量 AB 和 AP 的叉积
        cross_res = [0, 0, 0]

        AB_tuple = (AB.x, AB.y, AB.z)  # 将 Vector3 转换为元组
        AP_tuple = (AP.x, AP.y, AP.z)  # 将 Vector3 转换为元组
        vtk.vtkMath.Cross(AB_tuple, AP_tuple, cross_res)

        # 计算叉积的长度和 AB 的长度
        cross_res_length = vtk.vtkMath.Norm(cross_res)
        AB_length = vtk.vtkMath.Norm(AB_tuple)

        # 计算距离：叉积的长度除以 AB 的长度
        return cross_res_length / AB_length
